<template>
  <div class="task-newAddm">
    <el-steps :active="stepsState" finish-status="success" simple style="margin-top: 20px">
      <el-step title="任务信息"></el-step>
      <el-step title="子任务选择"></el-step>
      <el-step title="点位配置"></el-step>
      <el-step title="执行方式选择"></el-step>
    </el-steps>
    <div class="task-newAddm-con">
      <div class="fromed task-inf" v-if="stepsState==1">
        <el-form ref="elForm2" :model="formData1" :rules="rules2" size="medium" label-width="120px">
          <el-row :gutter="15">
            <el-col :span="24">
              <el-row :gutter="15">
                <el-col :span="24">
                  <el-form-item label="变电站：" prop="bdz">
                    <el-select
                      v-model="formData1.bdz"
                      filterable
                      placeholder="请选择"
                      clearable
                      :style="{width: '100%'}"
                      @change="changeStation"
                    >
                      <el-option
                        v-for="item in stationName_options"
                        :key="item.code"
                        :label="item.name"
                        :value="item.code"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
          <el-row :gutter="15">
            <el-col :span="24">
              <el-row :gutter="15">
                <el-col :span="24">
                  <el-form-item label="巡检类型：" prop="xjlx">
                    <el-select
                      v-model="formData1.xjlx"
                      placeholder="请选择"
                      clearable
                      :style="{width: '100%'}"
                      @change="changexjlx"
                    >
                      <el-option
                        v-for="item in maintancegroup_options"
                        :key="item.code"
                        :label="item.name"
                        :value="item.code"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
          <el-row :gutter="15">
            <el-col :span="24">
              <el-row :gutter="15">
                <el-col :span="24">
                  <el-form-item label="任务名称：" prop="rwmc">
                    <el-input v-model="formData1.rwmc" :style="{width: '100%'}"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
          <el-row :gutter="15">
            <el-col :span="24">
              <el-row :gutter="15">
                <el-col :span="24">
                  <el-form-item label="备注：" prop="bz">
                    <el-input v-model="formData1.bz" :style="{width: '100%'}"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="tableed bigTableStyle" v-if="stepsState==2">
        <el-table
          border
          :data="tableData"
          height="100%"
          :row-class-name="tableRowClassName"
          style="width: 100%;height:100%;"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection"></el-table-column>
          <el-table-column type="index" label="序号" width="80"></el-table-column>
          <el-table-column prop="RWMC" label="子任务名称" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="JQRID" label="机器人编号"></el-table-column>
          <el-table-column prop="ZXSK" label="执行时刻"></el-table-column>
          <el-table-column prop="ZXRQ" label="执行日期"></el-table-column>
          <el-table-column prop="RWZT" label="任务状态"></el-table-column>
        </el-table>
      </div>
      <div class="fromed" v-if="stepsState==3">
        <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="120px">
          <el-row :gutter="15">
            <el-col :span="12">
              <el-row :gutter="15">
                <el-col :span="24">
                  <el-form-item label="区域名称：" prop="qymc">
                    <el-select
                      v-model="formData.qymc"
                      placeholder
                      clearable
                      :style="{ width: '100%' }"
                      @change="getDwpzSsjgmc"
                    >
                      <el-option
                        v-for="(item, index) in dwpzSsqymcList"
                        :key="index"
                        :label="item.QYMC"
                        :value="item.QYID"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-col>
            <el-col :span="12">
              <el-row :gutter="15">
                <el-col :span="24">
                  <el-form-item label="间隔名称：" prop="jgmc">
                    <el-select
                      v-model="formData.jgmc"
                      placeholder
                      clearable
                      :style="{ width: '100%' }"
                      @change="getDwpzSssbmc"
                    >
                      <el-option
                        v-for="(item, index) in dwpzSsjgmcList"
                        :key="index"
                        :label="item.JGMC"
                        :value="item.JGID"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
          <el-row :gutter="15">
            <el-col :span="12">
              <el-row :gutter="15">
                <el-col :span="24">
                  <el-form-item label="设备名称：" prop="sbmc">
                    <el-select
                      v-model="formData.sbmc"
                      placeholder
                      clearable
                      :style="{ width: '100%' }"
                      filterable
                    >
                      <el-option
                        v-for="(item, index) in dwpzSssbmcList"
                        :key="index"
                        :label="item.SBMC"
                        :value="item.SBID"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-col>
            <el-col :span="12">
              <el-row :gutter="15">
                <el-col :span="24">
                  <div class="fromed-btn">
                    <el-button type="primary" @click="search">查询</el-button>
                  </div>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </el-form>
        <div class="tree">
          <el-tree
            ref="tree"
            :data="newTreeData"
            show-checkbox
            node-key="id"
            :props="defaultProps"
            @check-change="handleNodeClick"
            :filter-node-method="filterNode"
            icon-class="custom"
            v-loading="loadinghandel"
            element-loading-text="拼命加载中"
            element-loading-spinner="el-icon-loading"
            element-loading-background="rgba(0, 0, 0, 0.5)"
          ></el-tree>
        </div>
      </div>
      <div class="fromed task-inf" v-if="stepsState==4">
        <el-form ref="elForm3" :model="formData3" :rules="rules3" size="medium" label-width="120px">
          <el-row :gutter="15">
            <el-col :span="24">
              <el-form-item label="执行方式：" prop="zxfs">
                <el-radio-group v-model="formData3.zxfs" @change="changezxfs">
                  <el-radio label="1" name="1" v-if="xjlxname!='例行巡检'">立即执行</el-radio>
                  <el-radio label="2" name="2" v-if="xjlxname!='例行巡检'">定期执行</el-radio>
                  <el-radio label="3" name="3" v-if="xjlxname=='例行巡检' || xjlxname=='自定义巡检'">周期执行</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 例行巡检和自定义巡检下，显示执行模式 -->
          <el-row :gutter="15" v-if="xjlxname=='例行巡检' || xjlxname=='自定义巡检'">
            <el-col :span="24">
              <el-form-item label="执行模式：" prop="zxms">
                <el-radio-group v-model="formData3.zxms">
                  <el-radio label="1" name="1">间隔模式</el-radio>
                  <el-radio label="2" name="2">周模式</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 定期执行，显示 -->
          <el-row :gutter="15" v-if="formData3.zxfs==2">
            <el-col :span="24">
              <div class="crontabdom-con">
                <div class="crontabdom-item" v-for="(item, index) in zxrqlist" :key="index">
                  <div>执行日期：</div>
                  <div>
                    <el-date-picker v-model="zxrqlist[index]" type="datetime" placeholder="选择日期"></el-date-picker>
                  </div>
                  <div>
                    <span @click="add(index)" class="iconfont iconxinzeng"></span>
                    <span @click="dele(index)" class="iconfont iconshanchu"></span>
                  </div>
                </div>
              </div>
            </el-col>
          </el-row>
          <!-- 周期执行-间隔模式，显示 -->
          <el-row :gutter="15" v-if="(formData3.zxfs==3) && (formData3.zxms==1)">
            <el-col :span="20">
              <el-form-item class="rwgl" label="执行日期：" prop="zxrq">
                <el-date-picker
                  class="rwgl"
                  v-model="formData3.zxrq"
                  :style="{width: '100%'}"
                  type="datetime"
                  placeholder="选择日期"
                ></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 周期执行-间隔模式，才显示 -->
          <el-row :gutter="15" v-if="(formData3.zxfs==3) && (formData3.zxms==1)">
            <el-col :span="24">
              <el-form-item label="执行周期：" prop="zxzq">
                <el-input-number
                  v-model="formData3.zxzq"
                  controls-position="right"
                  :min="0"
                  :max="100"
                ></el-input-number>天
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 只有周期执行-周模式，才显示 -->
          <el-row :gutter="15" v-if="(formData3.zxfs==3) && (formData3.zxms==2)">
            <el-col :span="24">
              <el-form-item label="星期选择：" prop="zqweek">
                <el-checkbox-group v-model="formData3.zqweek">
                  <el-checkbox label="2">周一</el-checkbox>
                  <el-checkbox label="3">周二</el-checkbox>
                  <el-checkbox label="4">周三</el-checkbox>
                  <el-checkbox label="5">周四</el-checkbox>
                  <el-checkbox label="6">周五</el-checkbox>
                  <el-checkbox label="7">周六</el-checkbox>
                  <el-checkbox label="1">周日</el-checkbox>
                </el-checkbox-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <div class="task-newAddm-btn">
      <el-button type="primary" @click="prev" class="centerbox" v-if="stepsState!=1">上一步</el-button>
      <el-button type="primary" @click="savePoint" class="centerbox" v-if="stepsState==3">保存并同步</el-button>
      <el-button type="primary" @click="next" class="centerbox" v-if="stepsState!=4">下一步</el-button>
      <el-button type="primary" @click="save" class="centerbox" v-if="stepsState==4">保存</el-button>
      <el-button type="primary" @click="issued" class="centerbox" v-if="stepsState==4">任务下发</el-button>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { publicAPIModule } from "@/store/modules/public";
import { JointInspectionModule } from "@/store/modules/JointInspection";
import { ParameterManagementModule } from "@/store/modules/ParameterManagement";
import { rtvideo } from "@/views/AuxiliaryMonitor/components/map";
import { Tree } from "element-ui";
import { log } from "three";
import date from "@/utils/date";
import time from "@/utils/time";
import lodash from "lodash";
@Component
export default class newAddm extends Vue {
  loadinghandel = true;
  public formData = {
    qymc: "",
    jgmc: "",
    sbmc: "",
  };
  public rules = {
    qymc: [],
    jgmc: [],
    sbmc: [],
  };

  public rules2 = {
    bdz: [],
    xjlx: [],
    rwmc: [],
    bz: [],
  };

  public formData3 = {
    zxfs: 0,
    zxrq: "",

    zxzq: "",
    zxms: 0,
    zqweek: [], //传参的时候用-拼接
  };
  zxrqlist: any = [""];
  public rules3 = {
    zxfs: [],
    zxrq: [],
    zxzq: [],
    zxms: [],
    zqweek: [],
  };

  // 第一步
  public formData1 = {
    bdz: "",
    xjlx: "",
    rwmc: "",
    bz: "",
  };
  stationname = "";
  xjlxname = "";
  get stationName_options() {
    let SubstationList = JointInspectionModule.LocalCityList || [];
    return SubstationList;
  }
  get maintancegroup_options() {
    // console.log(JointInspectionModule.getXslxList);
    return JointInspectionModule.getXslxList;
  }
  changeStation(val) {
    this.stationname =
      this.stationName_options.filter((o) => o.code === val)[0].name || "";
    this.formData1.rwmc = this.stationname + this.xjlxname;
  }
  changexjlx(val) {
    this.xjlxname =
      this.maintancegroup_options.filter((o) => o.code === val)[0].name || "";
    this.formData1.rwmc = this.stationname + this.xjlxname;
  }

  //第二步
  public tableData = [];
  public SelectTable = [];
  public handleSelectionChange(val: []) {
    this.SelectTable = val;
  }
  public tableRowClassName({ row, rowIndex }: any): string {
    // console.log(row, rowIndex);
    if (rowIndex % 2 === 0) {
      return "row1";
    } else {
      return "row2";
    }
  }
  // 子任务列表
  public gettabledata() {
    var params = {
      bdzid: this.formData1.bdz,
      czzt: "xz",
      xjlx: this.formData1.xjlx,
    };
    (this as any).$services
      .getBdzrw({ data: params, method: "delete" })
      .then((result: any) => {
        if (result.data) {
          this.tableData = result.data;
        }
      })
      .catch((err: object) => {
        console.log(err);
      });
  }

  // 第三步
  public dwpzSsqymcList: any[] = [];
  // 区域下拉
  getDwpzSsqymc() {
    var params = {
      bdzId: this.formData1.bdz,
    };
    (this as any).$services
      .getDwpzSsqymc({ data: params })
      .then((result: any) => {
        if (result.data) {
          this.dwpzSsqymcList = result.data;
        }
      })
      .catch((err: object) => {
        console.log(err);
      });
  }
  public dwpzSsjgmcList: any[] = [];
  // 间隔下拉
  getDwpzSsjgmc() {
    var params = {
      bdzId: this.formData1.bdz,
      qyId: this.formData.qymc,
    };
    (this as any).$services
      .getDwpzSsjgmc({ data: params })
      .then((result: any) => {
        if (result.data) {
          this.dwpzSsjgmcList = result.data;
        }
      })
      .catch((err: object) => {
        console.log(err);
      });
  }
  public dwpzSssbmcList: any[] = [];
  // 设备下拉
  getDwpzSssbmc() {
    var params = {
      bdzId: this.formData1.bdz,
      qyId: this.formData.qymc,
      jgId: this.formData.jgmc,
    };
    (this as any).$services
      .getDwpzSssbmc({ data: params })
      .then((result: any) => {
        this.formData.sbmc = "";
        if (result.data) {
          this.dwpzSssbmcList = result.data;
        }
      })
      .catch((err: object) => {
        console.log(err);
      });
  }
  // 树图
  public treedata: any[] = [];
  filterformData: any = {};
  get newTreeData() {
    let treedata = lodash.cloneDeep(this.treedata);
    let qyid = this.filterformData.qymc;
    let jgid = this.filterformData.jgmc;
    let sbid = this.filterformData.sbmc;
    // treedata.forEach(bdz=>{
    //   let bdzChildren=bdz.children||[]
    //   if(bdzChildren.length){
    //     bdzChildren.forEach(qy=>{
    //       if(qy.type){

    //       }
    //     })
    //   }
    // })

    function dgpd(data) {
      if (data.children && data.children.length) {
        data.children.forEach((item, index, Arr) => {
          if (item.type === "qy") {
            if (qyid) {
              if (item.code !== qyid) {
                Arr.splice(index, 1);
              }
            }
          }
          if (item.type === "jg") {
            // console.log(111111, item);
            if (jgid) {
              if (item.code !== jgid) {
                Arr.splice(index, 1);
              }
            }
          }
          if (item.type === "sb") {
            if (sbid) {
              if (item.code !== sbid) {
                Arr.splice(index, 1);
              }
            }
          }
          dgpd(item);
        });
        return data;
      } else {
        return data;
      }
    }
    let bdzData = treedata.map((v) => {
      return dgpd(v);
    });
    console.log(bdzData);
    if (bdzData && bdzData.length > 0) {
      this.loadinghandel = false;
    }
    return bdzData;
  }
  public defaultProps = {
    label: "name",
    children: "children",
  };
  $refs!: {
    tree: Tree;
  };
  activeCD: any = [];

  initBdzDwTree() {
    var params = {
      bdzId: this.formData1.bdz,
      xjlx: this.formData1.xjlx,
    };
    (this as any).$services
      .initBdzDwTree({ data: params })
      .then((result: any) => {
        if (result.data) {
          this.treedata = result.data;
        }
      })
      .catch((err: object) => {
        console.log(err);
      });
  }

  // 节点点击事件
  handleNodeClick(data: any, checked) {
    let tree = this.$refs.tree;
    let cdFlagArr = [];
    if (tree) {
      let checkedNode = tree.getCheckedNodes();
      checkedNode.forEach((v: any) => {
        if (v.type) {
          if (v.type === "cd") {
            cdFlagArr.push(v.flag);
          }
        }
      });
    }
    this.activeCD = cdFlagArr;
  }
  // 查询按钮（过滤）
  public search() {
    this.filterformData = lodash.cloneDeep(this.formData);
    // if (this.formData.sbmc == "") {
    //   this.$message({
    //     type: "warning",
    //     message: "选择设备查询定位设备树",
    //   });
    // } else {
    //   let val = this.formData.sbmc;
    //   let tree = this.$refs.tree;
    //   // console.log(tree);
    //   if (tree) {
    //     tree.filter(val);
    //   }
    // }
  }
  filterNode(value: string, data: { name: string }) {
    if (!value) return true;
    return data.name.indexOf(value) !== -1;
  }

  // 步骤条相关
  public stepsState: number = 1;
  public buttomState = true;
  public nextStatus = false; //第三个下一步
  public saveStatus = false; //任务下发
  public rwid = ""; //保存并同步返回的任务id
  // 上一步
  prev() {
    if (this.stepsState < 2) {
      return;
    }
    if (this.stepsState == 3) {
      this.stepsState--;
    }
    if (this.stepsState == 4) {
      if (this.xjlxname !== "自定义巡检") {
        this.stepsState--;
      }
    }
    this.stepsState--;
  }
  // 下一步
  public next() {
    if (this.formData1.bdz == "") {
      this.$message({
        type: "warning",
        message: "请选择变电站",
      });
      return;
    }
    if (this.formData1.xjlx == "") {
      this.$message({
        type: "warning",
        message: "请选择巡检类型",
      });
      return;
    }
    if (this.stepsState == 1) {
      // 自定义巡检 不请求子任务列表
      if (this.xjlxname === "自定义巡检") {
        // 区域下拉框
        this.getDwpzSsqymc();
        // 树图
        this.initBdzDwTree();
        this.stepsState++;
      } else {
        this.gettabledata();
      }
      this.stepsState++;
    } else if (this.stepsState == 2) {
      this.getDwpzSsqymc();
      this.initBdzDwTree();
      if (this.SelectTable.length > 0) {
        if (this.xjlxname !== "自定义巡检") {
          this.stepsState++;
        }
        this.stepsState++;
      } else {
        this.$message({
          type: "warning",
          message: "请选择子任务",
        });
        return;
      }
    } else if (this.stepsState == 3) {
      // 自定义巡检 一定要先保存
      if (this.xjlxname === "自定义巡检" && !this.nextStatus) {
        this.$message({
          type: "warning",
          message: "请先保存并同步",
        });
      } else {
        this.stepsState++;
        this.buttomState = false;
      }
    }
  }
  // 保存并同步 自定义巡检才选测点
  savePoint() {
    // console.log(2231, this.activeCD);
    // console.log(this.xjlxname);

    if (this.xjlxname !== "自定义巡检") {
      return;
    }
    if (this.activeCD.length > 0) {
      // zdyxjParam
      var params = {
        czzt: "xz",
        bdzid: this.formData1.bdz,
        xjlx: this.formData1.xjlx,
        rwmc: this.formData1.rwmc,
        bz: this.formData1.bz,
        jg: [this.formData.jgmc],
        sb: [this.formData.sbmc],
        cd: this.activeCD,
      };

      (this as any).$services
        .saveAndSynchornizeDw({
          data: params,
          method: "post",
          // postparams: true,
        })
        .then((result: any) => {
          if (result.data) {
            this.rwid = result.data.rwid;
            this.nextStatus = true;
          }
          if (result.code == 200) {
            this.$message({
              showClose: true,
              message: result.message,
              type: "success",
            });
          } else {
            this.$message({
              showClose: true,
              message: result.message,
              type: "warning",
            });
          }
        })
        .catch((err: object) => {
          console.log(err);
          this.$message({
            showClose: true,
            message: `操作失败`,
            type: "warning",
          });
        });
    } else {
      this.$message({
        type: "warning",
        message: "请勾选测点数据",
      });
    }
  }

  add() {
    this.zxrqlist.push("");
  }
  dele(index: number) {
    if (this.zxrqlist.length == 1) {
      this.$message({
        type: "warning",
        message: "已经是最后一项了",
      });
    } else {
      this.zxrqlist.splice(index, 1);
    }
  }

  changezxfs() {
    console.log(this.formData3.zxfs);
  }

  //第四步的保存
  savedata: any = {}; //保存返回的id、rwid在里头
  public save() {
    let xjlx = this.formData1.xjlx;
    let zxms = this.formData3.zxms;
    let zxfs = this.formData3.zxfs; //判断用，不作为参数传递
    let zrwidlist = [];
    if (this.SelectTable.length > 0) {
      this.SelectTable.map((k, j) => {
        zrwidlist.push(k.ID);
      });
    }

    let params: any = {};
    if (zxfs == 0) {
      this.$message({
        type: "warning",
        message: "请勾选执行方式",
      });
      return;
    }

    let zxrqlist = this.zxrqlist;
    zxrqlist.map((k) => {
      k = date(k);
      return k;
    });
    // 例行只有周期执行，有间隔模式和周模式
    // 自定义 只有立即和定期 要加间隔、设备、测点字段
    // 专项特巡熄灯 只有立即和定期
    if (this.xjlxname == "例行巡检") {
      if (zxms == 1) {
        params = {
          czzt: "xz",
          bdzid: this.formData1.bdz,
          xjlx: "1",
          rwmc: this.formData1.rwmc,
          bz: this.formData1.bz,
          zrw: zrwidlist,
          zxms: "1",
          zq: [time(this.formData3.zxrq), this.formData3.zxzq],
          dq: [],
        };
        // result:{rwid=38a1b65cf17443849001e714ed6808bf, zq=9621, success=true}
      } else {
        params = {
          czzt: "xz",
          bdzid: this.formData1.bdz,
          xjlx: "1",
          rwmc: this.formData1.rwmc,
          bz: this.formData1.bz,
          zrw: zrwidlist,
          zxms: "2",
          zq: [this.formData3.zqweek.join("-"), "zxms2"],
          dq: [],
        };
      }
    } else if (this.xjlxname == "自定义巡检") {
      if (zxfs == 1) {
        params = {
          czzt: "xz",
          bdzid: this.formData1.bdz,
          xjlx: xjlx,
          rwmc: this.formData1.rwmc,
          bz: this.formData1.bz,
          jg: [this.formData.jgmc],
          sb: [this.formData.sbmc],
          cd: this.activeCD,
          rwid: this.rwid,
          zq: [],
          dq: [],
          lj: "lj",
        };
      } else if (zxfs == 2) {
        params = {
          czzt: "xz",
          bdzid: this.formData1.bdz,
          xjlx: xjlx,
          rwmc: this.formData1.rwmc,
          bz: this.formData1.bz,
          jg: [this.formData.jgmc],
          sb: [this.formData.sbmc],
          cd: this.activeCD,
          rwid: this.rwid,
          zq: [],
          dq: zxrqlist, 
        };
       
      } else if (zxfs == 3) {
        if (zxms == 1) {
          params = {
            czzt: "xz",
            bdzid: this.formData1.bdz,
            xjlx: xjlx,
            rwmc: this.formData1.rwmc,
            bz: this.formData1.bz,
            jg: [this.formData.jgmc],
            sb: [this.formData.sbmc],
            cd: this.activeCD,
            rwid: this.rwid,
            zxms: "1",
            zq: [time(this.formData3.zxrq), this.formData3.zxzq],
            dq: [],
          };
        } else {
          params = {
            czzt: "xz",
            bdzid: this.formData1.bdz,
            xjlx: xjlx,
            rwmc: this.formData1.rwmc,
            bz: this.formData1.bz,
            jg: [this.formData.jgmc],
            sb: [this.formData.sbmc],
            cd: this.activeCD,
            rwid: this.rwid,
            zxms: "2",
            zq: [this.formData3.zqweek.join("-"), "zxms2"],
            dq: [],
          };
        }
      }
    } else {
      if (zxfs == 1) {
        params = {
          czzt: "xz",
          bdzid: this.formData1.bdz,
          xjlx: xjlx,
          rwmc: this.formData1.rwmc,
          bz: this.formData1.bz,
          zrw: zrwidlist,
          zq: [],
          dq: [],
          lj: "lj",
        };
      } else if (zxfs == 2) {
        params = {
          czzt: "xz",
          bdzid: this.formData1.bdz,
          xjlx: xjlx,
          rwmc: this.formData1.rwmc,
          bz: this.formData1.bz,
          zrw: zrwidlist,
          zq: [],
          dq: zxrqlist,
        };
      }
    }
     var times=[]
        if (zxrqlist &&zxrqlist.length>0) {
          zxrqlist.map(v=>{
            times.push(time(v))
          })
        }
        params.dq=times;
    console.log(params);

    (this as any).$services
      .xzRwSave({
        data: { xzrwParam: params },
        method: "post",
        postparams: true,
      })
      .then((result: any) => {
        console.log(result);
        if (result.data) {
          this.savedata = result.data;
          this.$message({
            type: "success",
            message: "保存成功",
          });
          this.saveStatus = true;
        }
      })
      .catch((err: object) => {
        console.log(err);
        this.$message({
          type: "warning",
          message: "保存失败",
        });
      });
  }
  // 下发相关
  public issued() {
    // console.log('this.savedata:',this.savedata);
    const loading = this.$loading({
      lock: true,
      text: "loading",
      spinner: "el-icon-loading",
      background: "rgba(0,0,0,.7)",
    });

    if (!this.saveStatus) {
      this.$message({
        type: "warning",
        message: "请先保存",
      });
      loading.close();
    } else {
      let xjlx = this.formData1.xjlx;
      let zxms = this.formData3.zxms;
      let zxfs = this.formData3.zxfs;
      let savedata = this.savedata;
      let zrwidlist = [];
      if (this.SelectTable.length > 0) {
        this.SelectTable.map((k, j) => {
          zrwidlist.push(k.ID);
        });
      }
      let params: any = {};
      let zxrqlist = this.zxrqlist;
      let dqlist = [];
      zxrqlist.forEach((k, j) => {
        dqlist[j] = {
          id: savedata.lj,
          time: time(k),
        };
      });
      // console.log(xjlx,zxms,zxfs,savedata,zxrqlist);
      if (this.xjlxname == "例行巡检") {
        // 间隔模式
        if (zxms == 1) {
          params = {
            czzt: "xz",
            bdzid: this.formData1.bdz,
            xjlx: "1",
            rwmc: this.formData1.rwmc,
            bz: this.formData1.bz,
            zrw: zrwidlist,
            zxms: "1",
            zq: [
              date(this.formData3.zxrq),
              this.formData3.zxzq,
              this.savedata.lj,
            ],
            dq: [],
            rwid: this.savedata.rwid,
          };
          // 周模式
        } else {
          params = {
            czzt: "xz",
            bdzid: this.formData1.bdz,
            xjlx: "1",
            rwmc: this.formData1.rwmc,
            bz: this.formData1.bz,
            zrw: zrwidlist,
            zxms: "2",
            zq: [this.formData3.zqweek.join("-"), "zxms2", this.savedata.lj],
            dq: [],
            rwid: this.savedata.rwid,
          };
        }
      } else if (this.xjlxname == "自定义巡检") {
        // 立即
        if (zxfs == 1) {
          params = {
            czzt: "xz",
            bdzid: this.formData1.bdz,
            xjlx: xjlx,
            rwmc: this.formData1.rwmc,
            bz: this.formData1.bz,
            jg: [this.formData.jgmc],
            sb: [this.formData.sbmc],
            cd: this.activeCD,
            rwid: this.savedata.rwid,
            zq: [],
            dq: [],
            lj: this.savedata.lj,
          };
          // 定期
        } else if (zxfs == 2) {
          params = {
            czzt: "xz",
            bdzid: this.formData1.bdz,
            xjlx: xjlx,
            rwmc: this.formData1.rwmc,
            bz: this.formData1.bz,
            jg: [this.formData.jgmc],
            sb: [this.formData.sbmc],
            cd: this.activeCD,
            rwid: this.savedata.rwid,
            zq: [],
            dq: dqlist,
          };
        } else if (zxfs == 3) {
          if (zxms == 1) {
            params = {
              czzt: "xz",
              bdzid: this.formData1.bdz,
              xjlx: xjlx,
              rwmc: this.formData1.rwmc,
              bz: this.formData1.bz,
              jg: [this.formData.jgmc],
              sb: [this.formData.sbmc],
              cd: this.activeCD,
              rwid: this.savedata.rwid,
              zxms: "1",
              zq: [
                date(this.formData3.zxrq),
                this.formData3.zxzq,
                this.savedata.lj,
              ],
              dq: [],
            };
          } else {
            params = {
              czzt: "xz",
              bdzid: this.formData1.bdz,
              xjlx: xjlx,
              rwmc: this.formData1.rwmc,
              bz: this.formData1.bz,
              jg: [this.formData.jgmc],
              sb: [this.formData.sbmc],
              cd: this.activeCD,
              rwid: this.savedata.rwid,
              zxms: "2",
              zq: [this.formData3.zqweek.join("-"), "zxms2", this.savedata.lj],
              dq: [],
            };
          }
        }
      } else {
        if (zxfs == 1) {
          params = {
            czzt: "xz",
            bdzid: this.formData1.bdz,
            xjlx: xjlx,
            rwmc: this.formData1.rwmc,
            bz: this.formData1.bz,
            zrw: zrwidlist,
            zq: [],
            dq: [],
            lj: this.savedata.lj,
            rwid: this.savedata.rwid,
          };
        } else if (zxfs == 2) {
          params = {
            czzt: "xz",
            bdzid: this.formData1.bdz,
            xjlx: xjlx,
            rwmc: this.formData1.rwmc,
            bz: this.formData1.bz,
            zrw: zrwidlist,
            zq: [],
            dq: dqlist,
            rwid: this.savedata.rwid,
          };
        }
      }

      (this as any).$services
        .xzrwRwxfButton({
          data: { xzrwParam: params },
          method: "post",
          postparams: true,
        })
        .then((result: any) => {
          if (result.data) {
            this.$message({
              type: "success",
              message: `${result.message}`,
            });
            this.$emit("closeInit");
            this.$emit("close");
            loading.close();
          }
        })
        .catch((err: object) => {
          console.log(err);
          this.$message({
            type: "warning",
            message: "下发失败",
          });
          loading.close();
        });
    }
  }
}
</script>

<style lang="scss" scoped>
.task-newAddm {
  width: 100%;
  height: 500px;
  &-con {
    width: 100%;
    height: 400px;
    .fromed {
      width: 100%;
      height: 100%;
      padding: 0.25rem 0;
      .tree {
        height: 240px;
        overflow-y: auto;
      }
      &-btn {
        width: 100%;
        display: flex;
        justify-content: flex-end;
      }
    }
    .tableed {
      width: 100%;
      height: 100%;
    }
    .task-inf {
      width: 50%;
      margin: auto;
    }
  }
  .crontabdom-item {
    display: flex;
    justify-content: center;
    margin: 0.2rem 0;
    > div {
      line-height: 0.38rem;
      &:nth-child(3) {
        display: flex;
        justify-content: space-around;
        align-items: center;
        width: 1rem;
        .iconfont {
          font-size: 0.24rem;
          cursor: pointer;
        }
      }
    }
  }
  .crontabdom-con {
    height: 300px;
    overflow-y: auto;
  }
  &-btn {
    width: 100%;
    display: flex;
    justify-content: flex-end;
  }
}
</style>

<style lang="scss">
.task-newAddm {
  .el-step__head.is-success {
    color: #0c96fc;
    border-color: #05affd;
  }
  .el-step__title.is-success {
    color: #0c96fc;
  }
}
</style>

